package com.yc.sole.erp.controller;

import com.yc.sole.erp.biz.CategoryAttributeBiz;
import com.yc.sole.erp.request.CategoryAttributeQueryReq;
import com.yc.sole.erp.request.CategoryAttributeReq;
import com.yc.sole.erp.response.CategoryAttributeResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 类别属性
 *
 * @author yizuomin
 * @date 2024-01-13
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class CategoryAttributeController {

    private final CategoryAttributeBiz categoryAttributeBiz;

    /**
     * 新增
     *
     * @param categoryAttributeReq
     * @return RestResult
     */
    @PreAuth(value = "erp:categoryAttribute:save", description = "新增分类属性")
    @PostMapping("/api/erp/categoryAttribute/saveCategoryAttribute")
    public Result<Void> saveCategoryAttribute(@RequestBody @Validated(SaveGroup.class) CategoryAttributeReq categoryAttributeReq) {
        categoryAttributeBiz.saveCategoryAttribute(categoryAttributeReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param categoryAttributeReq
     * @return RestResult
     */
    @PreAuth(value = "erp:categoryAttribute:update", description = "修改分类属性")
    @PostMapping("/api/erp/categoryAttribute/updateCategoryAttributeById")
    public Result<Void> updateCategoryAttributeById(@RequestBody @Validated(UpdateGroup.class) CategoryAttributeReq categoryAttributeReq) {
        categoryAttributeBiz.updateCategoryAttributeById(categoryAttributeReq);
        return Result.success();
    }

    /**
     * 通过Id查询分类属性
     *
     * @param categoryAttrId
     * @return RestResult
     */
    @PreAuth(value = "erp:categoryAttribute:select", description = "通过Id查询分类属性")
    @PostMapping("/api/erp/categoryAttribute/selectCategoryAttributeById/{categoryAttrId}")
    public Result<CategoryAttributeResp> selectCategoryAttributeById(@PathVariable Long categoryAttrId) {
        return Result.data(categoryAttributeBiz.selectCategoryAttributeById(categoryAttrId));
    }

    /**
     * 通过id批量删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "erp:categoryAttribute:delete", description = "删除分类属性")
    @PostMapping("/api/erp/categoryAttribute/deleteCategoryAttribute")
    public Result<Void> deleteCategoryAttribute(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        categoryAttributeBiz.deleteCategoryAttributeByIds(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 通过分类查询
     *
     * @param categoryId
     * @return RestResult
     */
    @PreAuth(value = "erp:categoryAttribute:select", description = "通过分类id查询分类属性")
    @PostMapping("/api/erp/categoryAttribute/selectByCategoryId/{categoryId}")
    public Result<List<CategoryAttributeResp>> selectByCategoryId(@PathVariable Long categoryId) {
        return Result.data(categoryAttributeBiz.selectByCategoryId(categoryId));
    }


    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "erp:categoryAttribute:select", description = "查询分类属性")
    @PostMapping("/api/erp/categoryAttribute/selectCategoryAttributePage")
    public Result<Paging<CategoryAttributeResp>> selectCategoryAttributePage(@RequestBody @Validated BasePageReq<CategoryAttributeQueryReq> basePageReq) {
        return Result.data(categoryAttributeBiz.selectCategoryAttributePage(basePageReq.toPage(), basePageReq.getQuery()));
    }
}
